<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<c:set var="ctx" value="${pageContext.request.contextPath}"/>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>发送通知</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<script src="${ctx}/rs/js/jquery.validate.min.js"></script>
<script src="${ctx}/rs/js/jquery.form.js" type="text/javascript"></script>
</head>
<body>
<form id="form1" class="form-horizontal" action="${ctx}/notice/save.htm" method="post">
      <div class="form-group">
        <label for="name" class="col-sm-2 control-label item ">通知内容：</label>
        <div class="col-sm-6  ">
          <textarea class="form-control" rows="3" id="notice_content" name="notice_content" placeholder="输入内容不超过50个字"/>
        <span class="help-block col-sm-4" id="valierr" style="color:#FF9966">*</span>
        </div>
      </div>

      <div class="form-group">
        <label for="name" class="col-sm-2 control-label item ">通知期限：</label>
        <div class="col-sm-6  ">
           <input type="text"  class="form-control timeput Wdate2" name="start_time" id="start_time"
             onFocus="var endDate=$dp.$('end_time');WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss',onpicked:function(){end_time.focus();},minDate:'%y-%M-%d',maxDate:'#F{$dp.$D(\'end_time\')}'})"/>
            - 
           <input type="text"  class="form-control timeput Wdate2" name="end_time" id="end_time" 
             onFocus="WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss',minDate:'#F{$dp.$D(\'start_time\')}'})"/>
        </div>
      </div>
      
      <div class="form-group">
        <label for="name" class="col-sm-2 control-label item ">发布位置：</label>
        <div class="col-sm-6  ">
          <div class="wid400" id="days">
           <label class="checkbox-inline">
                <input type="checkbox" name="weeks" id="day1" value="1" />广告主平台
            </label>
            <label class="checkbox-inline">
                <input type="checkbox" name="weeks" id="day2" value="2" />媒体主平台
            </label>
            <label class="checkbox-inline">
                <input type="checkbox" name="weeks" id="day3" value="3" />平台首页
            </label>
            <label class="checkbox-inline">
                <input type="checkbox" name="weeks" id="day4" value="4" disabled="true"/>邮件通知媒体主
            </label>
            <label class="checkbox-inline">
                <input type="checkbox" name="weeks" id="day5" value="5" disabled="true"/>邮件通知广告主
            </label>
            <label class="checkbox-inline">
                <input type="checkbox" name="weeks" id="day6" value="6"  disabled="true"/>邮件通知广告代理商
            </label>
<!--             <label class="checkbox-inline"> -->
<!--                 <input type="checkbox"  id="allday" />全部 -->
<!--                 <br /> -->
<!--             </label><br /> -->
            </div>
        </div>
        <span class="help-block col-sm-4" id="valierr" style="color:#FF9966">*</span>
      </div>
      
      <div class="form-group">
        <div class="col-sm-10 text-center pt15">
          <shiro:hasPermission name="message:save">
             <button type="button" class="btn btn-primary btn-lg srt-btn active mr30" onclick="valiSubmit()">确定并发布</button>
             <button type="reset" class="btn btn-default btn-lg srt-btn active">重置</button>
          </shiro:hasPermission>
        </div>
      </div>

      <div class="form-group">
        <div class=" col-sm-10">
          <span id="error-text" style="color: #FF0000;"></span>
        </div>
      </div>
</form>


<script type="text/javascript" src="${ctx}/rs/js/bootstrapQ.min.js"></script>
<script src="${ctx}/rs/js/messages_zh.min.js"></script>
<script type="text/javascript">

$("#days input[name=weeks]").on("change",function(){
    var s = $("#days input[name=weeks]:checked").size();
    if(s==6){
        $("#allday")[0].checked=false;
        return;
    }
    if(!$(this)[0].checked){
        $("#allday")[0].checked=false;
   }
});


$("#allday").on("click",function()
   {
      if($("#allday")[0].checked)
       {
          $("#days label input").each(function(){
              $(this)[0].checked = true;
          });
       }else
       {
           $("#days label input").each(function(){
               $(this)[0].checked = false;
           });
       }
   });
    //初始化验证 start
    var form = $('#form1');
    form.validate({
        rules: {
            title: {
                maxlength: 50,
                required: true
            },
            content: {
                maxlength: 500,
                required: true
            }
          },
          highlight: function(element) {
              $(element).closest('.form-group').removeClass('success').addClass('error');
          },
          success: function(element) {
              element.text('OK!').addClass('valid')
              .closest('.form-group').removeClass('error').addClass('success');
          }
    });
    /** end */

    function valiSubmit() {
    	   var dayWeek = [];
           $("[name='weeks']").each(function() {
               if ($(this).is(":checked")) {
                   dayWeek.push($(this).val());
               }
           });
        if (form.valid()) {
            var notice_content = $("#notice_content").val();      //获取表单多个控件的数据
            var start_time = $("#start_time").val();
            var end_time = $("#end_time").val();
            var nn = dayWeek;
            if(notice_content =="") {
            	alert("请填写必填项");
                return ;
            }
            if(dayWeek.length== 0 ){
            	alert("请填写必填项");
                return ;
            }
            if(notice_content.length>50){
            	alert("内容不能超过50个字");
            	return ;
            }if(end_time=="" && start_time!=""){
            	alert("请填写结束时间");
            	return;
            }if(end_time!="" && start_time==""){
            	alert("请填写开始时间");
                return;
            }
            $(".unable").show();
            $.ajax({
                url: "${ctx}/notice/save.htm",
                type: "post",
                data: {
                	notice_content:notice_content,
                	start_time1:start_time,
                	end_time1:end_time,
                	week:dayWeek.join(",")
                },
                success: function (text) {
                    if(!$('.unable').is(':hidden')){
                        $(".unable").hide();
                    }
                    // 普通消息提示条
                    bootstrapQ.msg({
                        msg : '发送消息成功！',
                        type : 'success',
                        time : 2000
                    });
                    alert("发布成功");
                    //触发隐藏确认的按钮，引用回调方法关闭窗口并刷新列表
                    $("button.bsok").click();
                }
            });
        }
    }
         
    

</script>
</body>
</html>